<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="会员卡名称">
        <el-input style="width: 200px"  v-model="card.name"/>
      </el-form-item>
      <el-form-item label="会员卡类型">
        <el-select v-model="card.type" placeholder="请输入类型">
          <el-option label="次卡" :value="0"></el-option>
          <el-option label="储值类型" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="价格" v-if="card.type==0">
        <span>
          <el-input  style="width: 200px" v-model="card.price"></el-input>
        </span>
      </el-form-item>
      <el-form-item label="支持场馆">
        <el-select v-model="card.gymId" placeholder="请输入场馆">
          <el-option v-for="item in gym" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>

        <el-form-item label="背景图片">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:8080/upload/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="img" :src="img" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="edit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import card from "../../../api/card";
import gym from "../../../api/gym";
  export default {
    data(){
      return{
        card:{},
        saveBtnDisabled:false,
        dialogVisible: false,
        gym:[],
        img: "",
      }
    },
    created() {
      if(this.$route.params!=null&&this.$route.params.id!=null){
        this.queryById(this.$route.params.id);
      }
      gym.queryAll().then(res=>{
        this.gym=res.data;
      })
    },
    methods: {
      edit(){
        if(this.card.type!=0){
          this.card.price==0
        }
        if(this.$route.params!=null&&this.$route.params.id!=null){
          card.modify(this.card).then(res=>{
            this.$message.success(res.message)
            this.$router.push({path:`/account/card`})
          })
        }else{
          card.add(this.card).then(res=>{
            this.$message.success(res.message)
            this.$router.push({path:`/account/card`})
          })
        }
        },
      queryById(id){
        card.getById(id).then(res=>{
          this.card=res.data
          this.img = "http://rj0387v46.hn-bkt.clouddn.com/" + res.data.img;
        })
      },
      handleInputConfirm() {
        let inputValue = this.inputValue;
        if (inputValue) {
          this.dynamicTags.push(inputValue);
        }
        this.inputVisible = false;
        this.inputValue = '';
      },

      handleAvatarSuccess(res, file) {
        this.img = "http://rj0387v46.hn-bkt.clouddn.com/"+res.data;
        this.card.img=res.data;
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'||file.type === 'image/jpg';
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
